//
//  LoginViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "ActivityViewManager.h"
#import "PersonalViewController.h"
#import "ChangeInfoViewController.h"
#import "RootViewController.h"

@implementation LoginViewController


@synthesize username,password,autoLoginSwitch;
@synthesize name,pwd,isAuto;

#pragma mark - my own methods
- (IBAction)registerButtonClicked
{
    if ([self.username isFirstResponder]) {
        [self.username resignFirstResponder];
    }
    else if([self.password isFirstResponder]){
        [self.password resignFirstResponder];
    }
    
    ChangeInfoViewController *changeController = [[ChangeInfoViewController alloc] init];
    changeController.viewType = kViewAdd;
    UINavigationController *naviController = [[UINavigationController alloc] initWithRootViewController:changeController];
    naviController.navigationBar.tintColor = [UIColor darkGrayColor];
    [self presentModalViewController:naviController animated:YES];
    [changeController release];
    [naviController release];
}

- (BOOL)checkAndSaveUsernameWithPassword
{
    if(nil == self.username.text || [self.username.text isEqualToString:@""])
        return NO;
    else if(nil == self.password.text || [self.password.text isEqualToString:@""])
        return NO;
    else
        return YES;
}

#pragma AbstractTabViewController methods
- (IBAction)leftItemClicked
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)rightItemClicked
{
    
    if ([self.username isFirstResponder]) {
        [self.username resignFirstResponder];
    }
    else if([self.password isFirstResponder]){
        [self.password resignFirstResponder];
    }
    
    if(![self checkAndSaveUsernameWithPassword])
    {
        [ActivityViewManager showErrorViewWithMessage:@"用户名密码不能为空！"];
        return;
    }
    PersonalViewController *personalController = nil;
    id viewController;
    if(SYSTEW_VERSION_LESS_THAN(@"5.0"))
        viewController = [self parentViewController];
    else
        viewController = [self presentingViewController];
    
    RootViewController *rc = (RootViewController *)viewController;
    UINavigationController *nc = [[rc viewControllers] objectAtIndex:1];
    [nc popToRootViewControllerAnimated:NO];
    personalController = (PersonalViewController *)[nc topViewController];
    personalController.username = self.username.text;
    personalController.password = self.password.text;
    personalController.isAutoLogin = self.autoLoginSwitch.on;
    [personalController tabClicked:personalController.comprehensiveButton];
    //[personalController leftItemClicked];
}

#pragma mark - View lifecycle
- (void)dealloc
{
    [name release];
    [pwd release];
    [autoLoginSwitch release];
    [username release];
    [password release];

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    // Do any additional setup after loading the view from its nib.
    [super viewDidLoad];
    [self setBackgroundImageViewForPresentedView];
    self.username.text = self.name;
    self.password.text = self.pwd;
    self.autoLoginSwitch.on = self.isAuto;
    if(self.isAuto)
    {
        //[self performSelector:@selector(rightItemClicked) withObject:nil afterDelay:0.0];
        [self rightItemClicked];
    }
    else
    {
        if(![self.username isFirstResponder])
        {
            [self.username becomeFirstResponder];
        }
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.password = nil;
    self.username = nil;
    self.autoLoginSwitch = nil;
}

@end
